<?php
class Common {
	public static function _t(){
	    $arg_list = func_get_args();
	    if(!isset($GLOBALS['_LANG'])){
	    	return '';
	    }
	    $tmp = $GLOBALS['_LANG'];
	    foreach ($arg_list as $arg) {
	        if ($arg == 't' && !isset($tmp[$arg])) {
	            continue;
	        }
	        if ($arg == 'v' && !isset($tmp[$arg])) {
	            continue;
	        }
	        if (!isset($tmp[$arg])) {
	            return '';
	        }
	        $tmp = $tmp[$arg];
	    }
	    return $tmp;
	}
	
	public static function getStrValue($data,$param){
		if(empty($data)){
			return "";
		}
		if(isset($data[$param])&&$data[$param]!=""){
			return $data[$param];
		}
		return "";
	}
	
	public static function getPostStrValue($param){
		if(isset($_POST[$param])&&$_POST[$param]!=""){
			return $_POST[$param];
		}
		return "";
	}
	public static  function  getPostOrGet($k){
		if (isset($_POST[$k])){
			return $_POST[$k];
		}
		if (isset($_GET[$k])){
			return $_GET[$k];
		}
		return '';
	}
	public static function getGetStrValue($param,$default_value=""){
		if(isset($_GET[$param])){
			return $_GET[$param];
		}
		return $default_value;
	}
	
	public static function fetchPairOfList($n_id,$n_v,$list){
		$pair = array();
		if(isset($list)&&!empty($list)){
			foreach($list as $k=>$v){
				if(isset($v[$n_id])){
					$pair[$v[$n_id]]=$v[$n_v];
				}
			}
			unset($list);
		}
		return $pair;
	}
	
	public static function array_value_list($list, $name)
	{
		$value_list = array();
		foreach ($list as $item) {
			if (isset($item[$name])) {
				$value_list[] = $item[$name];
			}
		}
		return array_unique($value_list);
	}
	
	public static function executeSerialNumber($tag,$number,$seed){
		$seed_str =  str_pad($seed,$number,"0",STR_PAD_LEFT);
		
		return $tag.$seed_str;
	}
	
	public static function getTimes (){
		$time = array(
				1000=>array('name'=>'10:00','nextName'=>'10:25','time'=>'1000'),
				1030=>array('name'=>'10:30','nextName'=>'10:55','time'=>'1030'),
				1100=>array('name'=>'11:00','nextName'=>'11:25','time'=>'1100'),
				1130=>array('name'=>'11:30','nextName'=>'11:55','time'=>'1130'),
				1200=>array('name'=>'12:00','nextName'=>'12:25','time'=>'1200'),
				1230=>array('name'=>'12:30','nextName'=>'12:55','time'=>'1230'),
				1300=>array('name'=>'13:00','nextName'=>'13:25','time'=>'1300'),
				1330=>array('name'=>'13:30','nextName'=>'13:55','time'=>'1330'),
				1400=>array('name'=>'14:00','nextName'=>'14:25','time'=>'1400'),
				1430=>array('name'=>'14:30','nextName'=>'14:55','time'=>'1430'),
				1500=>array('name'=>'15:00','nextName'=>'15:25','time'=>'1500'),
				1530=>array('name'=>'15:30','nextName'=>'15:55','time'=>'1530'),
				1600=>array('name'=>'16:00','nextName'=>'16:25','time'=>'1600'),
				1630=>array('name'=>'16:30','nextName'=>'16:55','time'=>'1630'),
				1700=>array('name'=>'17:00','nextName'=>'17:25','time'=>'1700'),
				1730=>array('name'=>'17:30','nextName'=>'17:55','time'=>'1730'),
				1800=>array('name'=>'18:00','nextName'=>'18:25','time'=>'1800'),
				1830=>array('name'=>'18:30','nextName'=>'18:55','time'=>'1830'),
				1900=>array('name'=>'19:00','nextName'=>'19:25','time'=>'1900'),
				1930=>array('name'=>'19:30','nextName'=>'19:55','time'=>'1930'),
				2000=>array('name'=>'20:00','nextName'=>'20:25','time'=>'2000'),
				2030=>array('name'=>'20:30','nextName'=>'20:55','time'=>'2030'),
				2100=>array('name'=>'21:00','nextName'=>'21:25','time'=>'2100'),
				2130=>array('name'=>'21:30','nextName'=>'21:55','time'=>'2130'),
				2200=>array('name'=>'22:00','nextName'=>'22:25','time'=>'2200'),
				2230=>array('name'=>'22:30','nextName'=>'22:55','time'=>'2230'),
		);
		return $time;
	}
	
	public static function getTimesPairsArray (){
		$timePair = array();
		$timeArray = self::getTimes();
		foreach($timeArray as $k=>$v){
			$timePair[$k] = $v['name'].'-'.$v['nextName'];
		}
		return $timePair;
	}
		
	
	public static function getTimesValue($key){
		$timePair = array();
		$timeArray = self::getTimes();
		if(isset($timeArray[$key])){
			return $timeArray[$key]['name'].'-'.$timeArray[$key]['nextName'];
		}
		return "";
	}
	
	public static function getLangPostValue($zh_name,$en_name)
	{
		$ret = '';
		if (isset($zh_name)) {
			$ret .= '<lang=zh_cn>' . $zh_name . '</lang>';
		}
		if (isset($en_name)) {
			$ret .= '<lang=en_us>' . $en_name. '</lang>';
		}
		return $ret;
	}	

	public static function encrypt($txt, $key) {
		srand((double)microtime() * 1000000);
		$encrypt_key = md5(rand(0, 32000));
		$ctr = 0;
		$tmp = '';
		for($i = 0;$i < strlen($txt); $i++) {
			$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
			$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
		}
		return base64_encode(self::crypt_key($tmp, $key));
	}
	
	public static function  decrypt($txt, $key) {
		$txt = self::crypt_key(base64_decode($txt), $key);
		$tmp = '';
		for($i = 0;$i < strlen($txt); $i++) {
			$md5 = $txt[$i];
			$tmp .= $txt[++$i] ^ $md5;
		}
		return $tmp;
	}
	
	public  static function crypt_key($txt, $encrypt_key) {
		$encrypt_key = md5($encrypt_key);
		$ctr = 0;
		$tmp = '';
		for($i = 0; $i < strlen($txt); $i++) {
			$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
			$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
		}
		return $tmp;
	}
	
	public static function encryptUrl($str,$key='1019'){
		$str = serialize($str);
		$encrypt_url = self::encrypt($str,$key);
		$encrypt_url = urlencode($encrypt_url);
		return $encrypt_url;
	}
	
	public static function decryptUrl($parameters_str,$key='1019'){
		$parameters_str = urldecode($parameters_str);
		$decrypt_url = self::decrypt($parameters_str,$key);
		$decrypt_url = unserialize($decrypt_url);
		return $decrypt_url;
	}
	
	
	public static function getDefaultBeforeOpenTime(){
		$hour = date('H');
		$old_minute = date('i');
		$minute =$old_minute;
		$defaultBookTime = $hour.$minute;
			
		if(floor($old_minute/30)==0){
			$minute = '00';
			if($hour<=21){
				$hour = $hour+1;
			}
			$defaultBookTime = $hour.$minute;
		}elseif(floor($old_minute/30)==1){
			$minute = '30';
			if($hour<=21){
				$hour = $hour+1;
			}
		}
		$defaultBookTime = $hour.$minute;
		return $defaultBookTime;
	}
	
	public static function getDefaultOpenTime(){
		$hour = date('H');
		$old_minute = date('i');
		$minute =$old_minute;
		$defaultBookTime = $hour.$minute;
			
		if(floor($old_minute/30)==0){
			$minute = '30';
			$hour = $hour;
			$defaultBookTime = $hour.$minute;
		}elseif(floor($old_minute/30)==1){
			$minute = '00';
			if($hour<=21){
				$hour = $hour+1;
			}else{
				$hour='22';
				$minute = '30';//最后一班
			}
		}
		$defaultBookTime = $hour.$minute;
		return $defaultBookTime;
	}
	
	//提前一个小时
	public static function getDefaultNextOpenTime(){
		$hour = date('H');
		$old_minute = date('i');
		$minute =$old_minute;
		$defaultBookTime = $hour.$minute;
			
		if(floor($old_minute/30)==0){
			$minute = '30';
			$hour = $hour+1;
			$defaultBookTime = $hour.$minute;
		}elseif(floor($old_minute/30)==1){
			if($hour<=21){
				$minute = '00';
				$hour = $hour+2;
			}
		}
		$defaultBookTime = $hour.$minute;
		return $defaultBookTime;
	}
	
	//提前半个小时
	public static function getDefaultNextOpenTimeByHalf(){
		$hour = date('H');
		$old_minute = date('i');
		$minute =$old_minute;
		$defaultBookTime = $hour.$minute;

		$hour = $hour+1;
		if(floor(($old_minute+30)/60)==0){
			$minute = '00';
		}else{
			$minute = '30';
		}

		$defaultBookTime = $hour.$minute;
		return $defaultBookTime;
	}
	

	public static  function escapeArray(&$string, $low = False)
	{
		if (! is_array ( $string ))
		{
			$string = trim ( $string );
			$string = strip_tags ( $string );
			$string = htmlspecialchars ( $string );
			if ($low)
			{
				return True;
			}
			$string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string );
			$no = '/%0[0-8bcef]/';
			$string = preg_replace ( $no, '', $string );
			$no = '/%1[0-9a-f]/';
			$string = preg_replace ( $no, '', $string );
			$no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';
			$string = preg_replace ( $no, '', $string );
			return True;
		}
		$keys = array_keys ( $string );
		foreach ( $keys as $key )
		{
			self::escapeArray ($string [$key]);
		}	
	}
	
	public static  function getCalandarNowMonth($checkDate){
		$month = array();
		$calendar = ClassManager::getCalendar();
		$y = date('Y');
		$m  = date('m');
		$month['y'] = $y;
		$month['m'] = $m;
		$month['date'] = $calendar->display($y,$m,$checkDate);
		return $month;
	}
	
	public static  function getCalandarNextMonth($checkDate){
		$nextMonth  = array();
		$calendar = ClassManager::getCalendar();
		$y = date('Y');
		$m  = date('m');
		$m = $m+1;
		if ($m>12){
			$y=$y+1;
			$m = 1;
		}
		if ($m<10){
			$m = '0'.$m;
		}
		$nextMonth['y'] = $y;
		$nextMonth['m'] = $m;
		$nextMonth['date'] = $calendar->display($y,$m,$checkDate);
		return $nextMonth;
	}

	public static  function is_number($str){
		if(!preg_match("/^[0-9]+$/",$str)){
			return false;
		}
		return true;
	}
		
	public static  function cacluteDetailOrderFund($userSale,$orderSale){
		if($userSale<=2000){
			return $orderSale*0.12;
		}

		if($userSale>2000&&$userSale<=5000){
			return 2000*0.12+($orderSale-2000)*0.14;
		}
		
		if($userSale>5000){
			return 2000*0.12+3000*0.14+($orderSale-10000)*0.15;
		}
	}
	
	public static  function cacluteSummxOrderFund($userSale,$orderSale){
		$sale_fund = 0;
		$sumSale = $userSale+$orderSale;
		if(($userSale+$orderSale)<=2000){
			$sale_fund = $orderSale*0.12;
			return $sale_fund;
		}
		if($sumSale>2000&&$sumSale<=5000){
			$sumSale_fund = 2000*0.12+($sumSale-2000)*0.14;
			if($userSale<=2000){
				$userSale_fund = $userSale*0.12;
			}else{
				$userSale_fund = ($userSale-2000)*0.14+2000*0.12;
			}
			$sale_fund = $sumSale_fund-$userSale_fund;
			return $sale_fund;
		}
		
		if($sumSale>5000){
			$sumSale_fund = 2000*0.12+3000*0.14+($sumSale-5000)*0.15;
			if($userSale<=2000){
				$userSale_fund = $userSale*0.12;
			}elseif($userSale>2000&&$userSale<=5000){
				$userSale_fund = 2000*0.12+($userSale-2000)*0.14;
			}else{
				$userSale_fund = 2000*0.12+3000*0.014+$userSale*0.15;
			}
			
			$sale_fund = $sumSale_fund-$userSale_fund;
			return $sale_fund;
		}
	}
}
?>